package com.ruoyi.framework.security.provider;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.security.provider.token.TouristAuthenticationToken;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.core.authority.mapping.NullAuthoritiesMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>三方登录认证</p>
 * <p>创建日期：2021-07-17</p>
 *
 * @author zhou-qf
 */
public class TouristAuthenticationProvider implements
        AuthenticationProvider {

    private GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();

    private ISysUserService sysUserService;

    private SysPermissionService permissionService;

    private ISysPostService sysPostService;

    public TouristAuthenticationProvider(ISysUserService sysUserService, SysPermissionService permissionService, ISysPostService sysPostService){
        this.sysUserService = sysUserService;
        this.permissionService = permissionService;
        this.sysPostService = sysPostService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        SysUser sysUser = new SysUser();
        sysUser.setUserCode(authentication.getPrincipal().toString());
        // 获取用户信息
        List<SysUser> list = sysUserService.selectUserList(sysUser);
        if(list.isEmpty()){
            throw new UsernameNotFoundException("用户不存在");
        }
        SysUser sysAppUser = list.get(0);

        Set<String> postCode = sysPostService.selectPostCodeByUserId(sysAppUser.getUserId());
        postCode = postCode.parallelStream().map( s ->  "GROUP_" + s).collect(Collectors.toSet());
        postCode.add("ROLE_ACTIVITI_USER");
        List<SimpleGrantedAuthority> collect = postCode.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList());
        // 转换Security user实体类
        LoginUser loginAppUser = new LoginUser(sysAppUser, permissionService.getMenuPermission(sysAppUser), collect);
        // 返回认证成功
        return createSuccessAuthentication(authentication, loginAppUser);
    }

    protected Authentication createSuccessAuthentication(Authentication authentication, UserDetails user) {

        TouristAuthenticationToken result = new TouristAuthenticationToken(
                user,
                authoritiesMapper.mapAuthorities(user.getAuthorities()));
        result.setDetails(authentication.getDetails());

        return result;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (TouristAuthenticationToken.class
                .isAssignableFrom(authentication));
    }
}
